﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RsCms.Application;
using RsCms.Application.Settings.Dto;
using RsCode;

namespace Plugin.RsCms.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SettingsController : ControllerBase
    {
        ISettingAppService settingAppService;IApplicationDbContext applicationDbContext;
        public SettingsController(ISettingAppService settingAppService,IApplicationDbContext applicationDbContext)
        {
            this.settingAppService = settingAppService;
            this.applicationDbContext = applicationDbContext;
        }
        /// <summary>
        /// 浏览设置
        /// </summary>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<object> GetListAsync(string name)
        {
            return await settingAppService.GetSettingListAsync(name);
        }

        [Authorize("admin")]
        [HttpPost("edit")]
        public async Task<object>SaveAsync(List<SettingEditDto> dto)
        {
            return await settingAppService.SaveAsync(dto);
        }

        [HttpGet("check/version")]
        public  Task<CheckVersionDto> CheckVersion(string version)
        {
            string CurrentVersion = "20241024";
            CheckVersionDto dto = new CheckVersionDto
            {
                Version = CurrentVersion,
                DownloadUrl = "",
                NewVersion = false,
                Database=applicationDbContext.Current.Ado.DataType.ToString(),
                Description ="使用AI生成爆款文章",
                 
        };
            if (version!=CurrentVersion)
            {
                dto.UpdateDate = "2024-10-24";
                dto.NewVersion = true;
                dto.DownloadUrl = $"https://rscode.cn/down/rscms-{CurrentVersion}.rar";
            }
            return Task.FromResult(dto);
        }

        [HttpGet("siteinfo")]
        public async Task<object>GetSiteInfoAsync()
        {
            return await settingAppService.GetSiteInfoAsync();

        }

        [HttpDelete("delete")]
        public async Task<object> DeleteAsync(string id)
        {
            await settingAppService.DeleteAsync(id);
            return "ok";
        }
    }
}
